<template>
  <div class="login-container">
    <div class="form-wrapper">
      <form class="auth-form" @submit.prevent="handleLogin">
        <h2 class="form-title">餐厅管理系统</h2>

        <!-- 用户名输入 -->
        <div class="input-group">
          <i class="icon fas fa-user"></i>
          <input
              v-model="form.username"
              type="text"
              placeholder="用户名"
              required
              class="form-input"
          />
        </div>

        <!-- 密码输入 -->
        <div class="input-group">
          <i class="icon fas fa-lock"></i>
          <input
              v-model="form.password"
              type="password"
              placeholder="密码"
              required
              class="form-input"
          />
        </div>

        <!-- 登录按钮 -->
        <button
            type="submit"
            class="submit-btn"
            :disabled="loading"
        >
          <template v-if="!loading">
            立即登录
            <i class="fas fa-arrow-right"></i>
          </template>
          <span v-else class="loading-text">
            <i class="fas fa-spinner fa-spin"></i>
            登录中...
          </span>
        </button>

        <div class="form-footer">
          <span>没有账号？</span>
          <a href="#" @click.prevent="$router.push('/register')">立即注册</a>
        </div>
      </form>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { userLogin } from '@/api/index.js'
import { useRouter } from 'vue-router'

const router = useRouter()
const form = ref({ username: '', password: '' })
const loading = ref(false)

// 修改后的handleLogin方法
const handleLogin = async () => {
  try {
    loading.value = true
    const res = await userLogin(form.value)
    
    if (res.success) {
      // 确保数据结构正确
      const { token, userId, username, isAdmin } = res.data
      
      localStorage.setItem('token', token)
      localStorage.setItem('userId', userId)
      localStorage.setItem('username', username)
      localStorage.setItem('isAdmin', isAdmin)
      console.log('登录成功', { token, userId, username, isAdmin })
      // 根据权限跳转
      const redirectPath = isAdmin ? '/admin' : '/user'
      router.push(redirectPath)
    } else {
      showMessage(res.message || '登录失败', 'error')
    }
  } catch (error) {
    showMessage(error.message || '登录异常', 'error')
  } finally {
    loading.value = false
  }
}

const showMessage = (text, type) => {
  const msgEl = document.createElement('div')
  msgEl.className = `alert-message ${type}`
  msgEl.innerHTML = `
    <i class="fas ${type === 'success' ? 'fa-check-circle' : 'fa-exclamation-circle'}"></i>
    ${text}
  `
  document.body.appendChild(msgEl)
  setTimeout(() => msgEl.remove(), 3000)
}
</script>

<style scoped lang="scss">
.login-container {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 1rem;
}

.form-wrapper {
  width: 100%;
  max-width: 400px;
}

.auth-form {
  background: rgba(255, 255, 255, 0.98);
  padding: 2rem;
  border-radius: 12px;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.1);
}

.form-title {
  text-align: center;
  margin-bottom: 2rem;
  color: #2d3748;
  font-size: 1.75rem;
  font-weight: 600;
}

.input-group {
  position: relative;
  margin: 0 auto 1.25rem;
  max-width: 280px;
}

.form-input {
  width: 100%;
  height: 40px;
  padding: 0.5rem 1rem 0.5rem 2.25rem;
  border: 2px solid #e2e8f0;
  border-radius: 20px;
  font-size: 0.9rem;
  transition: all 0.3s ease;

  &:focus {
    outline: none;
    border-color: #667eea;
    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.2);
  }
}

.icon {
  position: absolute;
  left: 0.8rem;
  top: 50%;
  transform: translateY(-50%);
  color: #718096;
  font-size: 0.9rem;
  z-index: 2;
}

.submit-btn {
  width: 100%;
  max-width: 280px;
  height: 40px;
  margin: 1.5rem auto 0;
  background: linear-gradient(135deg, #667eea, #764ba2);
  color: white;
  border: none;
  border-radius: 20px;
  font-size: 0.95rem;
  font-weight: 500;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  transition: all 0.3s ease;

  &:hover:not(:disabled) {
    opacity: 0.95;
    transform: translateY(-1px);
  }

  &:disabled {
    opacity: 0.7;
    cursor: not-allowed;
  }

  .fa-arrow-right {
    font-size: 0.8rem;
  }
}

.loading-text {
  display: flex;
  align-items: center;
  gap: 0.5rem;

  .fa-spinner {
    animation: spin 1s linear infinite;
  }
}

.form-footer {
  margin-top: 1.5rem;
  text-align: center;
  color: #718096;
  font-size: 0.9rem;

  a {
    color: #667eea;
    margin-left: 0.5rem;
    text-decoration: none;
    font-weight: 500;

    &:hover {
      color: #764ba2;
    }
  }
}

.alert-message {
  position: fixed;
  top: 1.5rem;
  right: 1.5rem;
  padding: 0.8rem 1.2rem;
  border-radius: 8px;
  color: white;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.9rem;

  &.success {
    background: #48bb78;
  }

  &.error {
    background: #f56565;
  }

  .fa {
    font-size: 1.1rem;
  }
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

@media (max-width: 480px) {
  .auth-form {
    padding: 1.5rem;
  }

  .form-title {
    font-size: 1.5rem;
    margin-bottom: 1.5rem;
  }

  .input-group {
    max-width: 260px;
  }

  .form-input {
    height: 38px;
    padding-left: 2rem;
  }

  .icon {
    left: 0.6rem;
  }
}
</style>